// Copyright 2024 Tvux
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package _080

// removeDuplicates
// 双指针
func removeDuplicates(nums []int) int {
	slow := 0
	for fast, v := range nums {
		// nums[slow-2] - 上上个应该保留的元素
		// v - 当前检查的元素，即 nums[fast]
		// 如果当前检查的元素跟上上个应该保留的元素一样，说明当前检查的元素应该被删除。
		// 如果不一样，则说明当前元素应该被移过去slow的位置。而slow-2是应该保留的。
		if fast < 2 || nums[slow-2] != v {
			nums[slow] = v
			slow++
		}
	}
	return slow
}

// removeDuplicates
// 使用map来充当一个哈希表，记录每个元素及其出现的次数
// 然后输出哈希表中的元素最多2次。
//func removeDuplicates(nums []int) int {
//	s := make(map[int]int)
//	for i := 0; i < len(nums); i++ {
//		s[nums[i]]++
//	}
//	res := make([]int, 0, len(nums))
//	for k, v := range s {
//		for i := 0; i < v && i < 2; i++ {
//			res = append(res, k)
//		}
//	}
//	sort.Ints(res)
//	copy(nums, res)
//	return len(res)
//}
